#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
void xiajiang();
void shangsheng(); 
int main() { 
st:
	int n = 0;
	cout<<"1.  2次平均降低率问题"<<endl;
	cout<<"2.  2次平均增长率问题"<<endl;
	cout<<"请输入序号"<<endl;
	cin>>n;
	switch(n) {
		case 1: xiajiang(); break;
		case 2: shangsheng(); break;
		default: cout<<"输入错误"<<endl; break;
	} 
	goto st;
	system("pause");
	return 0;
}
void xiajiang() {
	double m = 0, n = 0, x1 = 0, x2 = 0;
	cout<<"2次平均降低率问题"<<endl<<"请输入 起始数值 2次降低后的数值: ";
	cin>>m>>n;
	for (;;) {
		if (n < m) {
			cout<<m<<"(1-x)*(1-x)="<<n<<endl;
			break;
		} else {
			cout<<"\n"<<"数值错误，请重新输入"<<endl<<"请输入 起始数值 2次降低后的数值: ";
			cin>>m>>n;
		}
	}
	x1 = sqrt(n/m) - 1;
	x2 = -1 * sqrt(n/m) - 1;
	x1 = -1 * x1;
	x2 = -1 * x2;
	if (x1 > 1.0 || x1 < 0.0)
		cout<<"x1 = "<<x1<<"(舍去)"<<endl<<"x2 = "<<x2<<endl;
	if (x2 > 1.0 || x2 < 0.0)
		cout<<"x1 = "<<x1<<endl<<"x2 = "<<x2<<"(舍去)"<<endl;
	cout<<endl;
}
void shangsheng() {
	double m = 0, n = 0, x1 = 0, x2 = 0;
	cout<<"2次平均增长率问题"<<endl<<"请输入 起始数值 2次增长后的数值: ";
	cin>>m>>n;
	for (;;) {
		if (n > m) {
			cout<<m<<"(1+x)*(1+x)="<<n<<endl;
			break;
		} else {
			cout<<"\n"<<"数值错误，请重新输入"<<endl<<"请输入 起始数值 2次增长后的数值: ";
			cin>>m>>n;
		}
	}
	x1 = sqrt(n/m) - 1;
	x2 = -1 * sqrt(n/m) - 1;
	if (x1 > 1.0 || x1 < 0.0)
		cout<<"x1 = "<<x1<<"(舍去)"<<endl<<"x2 = "<<x2<<endl;
	if (x2 > 1.0 || x2 < 0.0)
		cout<<"x1 = "<<x1<<endl<<"x2 = "<<x2<<"(舍去)"<<endl;
	cout<<endl;
}